package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Sabrina
 * @Date:2025/06/13/17:00
 * @Description:
 */
@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "C端-用户地址接口")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息")
    public Result getAddresses(){
        log.info("查询当前登录用户的所有地址信息");
        List<AddressBook> list = addressBookService.getAddresses();
        return Result.success(list);
    }
    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result getDefaultAddress(){
        log.info("查询默认地址");
        AddressBook addressBook = addressBookService.getDefaultAddress();
        if(addressBook != null){
            return Result.success(addressBook);
        }else {
            return Result.error("没有查询到默认地址");
        }
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result getAddressById(@PathVariable Long id){
        log.info("根据id查询地址: {}", id);
        AddressBook addressBook = addressBookService.getAddressById(id);
        return Result.success(addressBook);
    }


    @PostMapping
    @ApiOperation("新增地址")
    public Result addAddr(@RequestBody AddressBook addressBook){
        addressBookService.addAddr(addressBook);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改地址")
    public Result update(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result delete(Integer id){
        addressBookService.delete(id);
        return Result.success();
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result updateDefault(@RequestBody AddressBook addressBook){
        addressBookService.updateDefault(addressBook);
        return Result.success();
    }

}
